package com.tmall.tmall.filter;

import com.alibaba.fastjson.JSONObject;
import com.tmall.tmall.pojo.Result;
import com.tmall.tmall.utils.JwtUtils;
import com.tmall.tmall.utils.ThreadLocalUtils;
import jakarta.servlet.*;
import jakarta.servlet.annotation.WebFilter;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ThreadUtils;

import java.io.IOException;
import java.util.Map;

@Slf4j
@WebFilter(urlPatterns = "/*")
public class LoginCheckFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest req= (HttpServletRequest) servletRequest;
        HttpServletResponse resp= (HttpServletResponse) servletResponse;
        //获取url
        String url=req.getRequestURL().toString();
        System.out.println(url);
        if(url.contains("login")||url.contains("register")||url.contains("upload")||url.contains("slideshow/list")
        ||url.contains("advertising/list")||url.contains("product/list")||url.contains("MainProductType/list")||
        url.contains("productType")){
            log.info("放行");
            filterChain.doFilter(servletRequest,servletResponse);
            return;
        }
        //是否有jwt令牌
        String jwt=req.getHeader("token");
        if(jwt==null || jwt.isEmpty()){
            log.info("无jwt");
            Result error = Result.error("没有登录");
            //转为json
            String notLogin = JSONObject.toJSONString(error);
            resp.getWriter().write(notLogin);
            return;
        }
        if(url.contains("admin")){
            try{
                Map<String, Object> map = JwtUtils.parseJWT(jwt);
                ThreadLocalUtils.setUid((Integer) map.get("id"));
                if(map.get("type").equals("admin")){
                    log.info("是管理员，放行");
                    filterChain.doFilter(servletRequest,servletResponse);
                }else{
                    log.info("普通用户");
                    Result error = Result.error("权限不足");
                    //转为json
                    String notLogin = JSONObject.toJSONString(error);
                    resp.getWriter().write(notLogin);
                    return;
                }
            }catch (Exception e){
                e.printStackTrace();
                log.info("令牌过期");
                Result error = Result.error("没有登录");
                //转为json
                String notLogin = JSONObject.toJSONString(error);
                resp.getWriter().write(notLogin);
                return;
            }
        }else{
            try{
                Map<String, Object> map = JwtUtils.parseJWT(jwt);
                ThreadLocalUtils.setUid((Integer) map.get("id"));
            }catch (Exception e){
                //解析失败
                e.printStackTrace();
                log.info("令牌过期");
                Result error = Result.error("没有登录");
                //转为json
                String notLogin = JSONObject.toJSONString(error);
                resp.getWriter().write(notLogin);
                return;
            }
            log.info("验证通过");
            filterChain.doFilter(servletRequest,servletResponse);
        }
    }
}
